* Set current directory and open log
cd "C:\Users\frank.haege\Documents\Research\EUPOL\Version 5\Data analysis"
capture log close
log using "prlx-v05-management02", replace text


***************************************************************************************
* Generation of variables for start date, type of instrument, and legislative procedure
***************************************************************************************


* Project and author information
********************************
* Project:		prlx
* Programme:	prlx-v05-management02.do
* Date:			10 October 2014
* Author:		Frank Haege, University of Limerick
* Contact:		frank.haege@ul.ie 

* Do-file description
*********************
* This do-file constructs variables for the start date of the decision-making process,
* the type of instrument, and the type of legislative procedure.
* Using the start date variable, the temporal domain of the dataset is
* restricted to proposals introduced between 1976 and 18th September 2014 (i.e. the download date).


* Clear memory  
**************
version 12.0
clear all
macro drop _all
set more off


* Load EUPOL v05 dataset
************************

* Load dataset
insheet using "prlx-v05-management01.csv", comma names case clear
des, short

* Delete variables that refer to documents and CELEX numbers
local v = "_celeNumb_ _documents_"
foreach i of local v {
	unab vars: *`i'*
	drop `vars'
	}
	
* Keep only variables that refer to relevant events
keep webno procCode propCode title runno legaBasi_original fielOfActi* typeOfFile* /*
	*/ procedures* adopByComm* tranToEp* tranToCoun* epCtteRepo1stRdg* epOpin1stRdg* /*
    */ epCtteRepoSingRdg* epOpinSingRdg* commPosiOnEpAmenOn1stRead* /*
	*/ commPosiOnEpAmenOnSingRead* adopAmenProp* polAgreCommPosi* adopCommPosi* /*
	*/ counAppr1stRdg* counAgre* formAdopByCoun* partAdopByCoun* replacement* /*
	*/ chanOfLegaBasiByComm* tranAmenPropToEp* tranAmenPropToCoun* signByEpAndCoun* /*
	*/ withByComm* rejeByCoun* noAdopByCoun* counAppr2ndRdg* partSignByEpAndCoun* /*
	*/ counDeciAt3rdRdg* epDeciOn3rdRdg* epOpin2ndRdg* concCommDeci* epDeciOn3rdRdg* epOpin2ndRdg*

* Save dataset
des, short
compress
save prlx-v05-management02a.dta, replace


* Load reduced dataset
clear
use prlx-v05-management02a.dta, clear

* Rename proposal code variable
rename propCode propcode
label var propcode "Proposal code"

* Rename procedure code variable
rename procCode proccode
label var proccode "Procedure code"

* Label variables
label var title "Proposal title"
label var webno "PreLex webpage numer"


* Restrict dataset to study time period
***************************************

* Correct obvious Prelex errors in Commission adoption information
replace adopByComm_date_0 = "29-10-1986" if adopByComm_date_0 == "29-10-0986"
replace adopByComm_date_0 = "08-09-1982" if adopByComm_date_0 == "08-09-1820"

* Generate Commission adoption date variable
generate dateadop = .
replace dateadop = date(adopByComm_date_0, "DMY")
label var dateadop "Date of adoption by Commission"

* Generate transmission to Council date variables
generate datetransc = .
replace datetransc = date(tranToCoun_date_0, "DMY")
label var datetransc "Date of transmission to Council"

* Generate transmission to EP date variable
generate datetranse = .
replace datetranse = date(tranToEp_date_0, "DMY")
label var datetranse "Date of transmission to EP"

* Generate start of decision-making process variable
generate startdate = .
replace startdate = dateadop
label var startdate "Start date"
tab startdate if startdate == ., m

* Use transmission to Council date if Commission adoption date is missing
replace startdate = datetransc if startdate == .
tab startdate if startdate == ., m

* Use transmission to EP date if Commission adoption and Council transmission date is missing
replace startdate = datetranse if startdate == .
tab startdate if startdate == ., m

* Generate year variable for start of decision-making process
generate year = year(startdate)
label var year "Start year"
tab year, m

* Generate year variable based on Commission proposal code
generate year3 = ""
replace year3 = regexs(0) if regexm(propcode, "[0-9][0-9][0-9][0-9]")
generate year2 = real(year3)
tab year2, m

* Inspect observations with missing start date
tab year2 if year == ., m
list webno propcode proccode title if year == . & year2 >= 2013 
* 171 of 936 proposals are missing in 2013 and 310 of 583 proposals in 2014 (as of 18th September)
* Most files are new and have not been populated with further information (yet)
list webno propcode proccode title if year == . & year2 < 2013 
* Looks like before 2013 the missing cases are mostly just errors in the database
* (e.g. duplicates or amended proposals)
list webno propcode proccode title if year == . & title != "NA" & year2 < 2013
* Before 2013: 4 error pages, 2 with an EP date (both Commission staff working papers), and 1 JAI Council recommendation
tab year2 if year == . & proccode != "NA", m 
list webno propcode proccode title if year == . & proccode != "NA" & year2 < 2013
* Two error pages

* Generate month variable for start of decision-making process
generate month = month(startdate)
label var month "Starth month"
tab month, m

* Inspect patterns of proposal introduction during the first years covered by Prelex
tab month if year == 1975
tab month if year == 1976
tab month if year == 1977
tab month if year == 1978
tab month if year == 1979
tab year, m
* number of proposals in 1975 is unusually low, especially in first half

* Drop observations outside the study period
drop if year <= 1975
tab year, m


* Generate legal instrument variable
************************************

* Generate Commission adoption type of file variable
rename adopByComm_0_typeOfFile_0 ftypeadop
replace ftypeadop = "" if ftypeadop == "NA"

* Generate transmission to Council type of file variable
rename tranToCoun_0_typeOfFile_0 ftypetrans
replace ftypetrans = "" if ftypetrans == "NA"

* Generate final Commission type of file variable
rename typeOfFile_commission_0 ftypecom
replace ftypecom = "" if ftypecom == "NA"

* Generate final Council type of file variable
rename typeOfFile_council_0 ftypecoun
replace ftypecoun = "" if ftypecoun == "NA"

* Compare type of file variables
tab ftypeadop, m
tab ftypetrans, m
tab ftypecom, m
tab ftypecoun, m

* Generate Commission adoption instrument variable 
generate instadop = .
* Generate transmission to Council instrument variable
generate insttrans = .
* Generate final Commission instrument variable
generate instcom = .
* Generate final Council instrument variable
generate instcoun = .

* Define labels for instrument variable values
label def instrumentl /*
	*/ 0 "Non-legislative proposal" /*
	*/ 1 "Decision" /*
	*/ 2 "Regulation" /*
	*/ 3 "Directive" /*
	*/ 4 "Report" /*
	*/ 5 "Declaration" /*
	*/ 6 "Protocol" /*
	*/ 7 "Recommendation" /*
	*/ 8 "Resolution" /*
	*/ 9 "Green paper" /*
	*/ 10 "Letter" /*
	*/ 11 "Letter of amendment" /*
	*/ 12 "Memorandum" /*
	*/ 13 "Opinion" /*
	*/ 14 "Periodic report" /*
	*/ 15 "Preliminary draft supplementary budget" /*
	*/ 16 "Preliminary draft budget" /*
	*/ 17 "Programme" /*
	*/ 18 "Framework decision" /*
	*/ 19 "Assent" /*
	*/ 20 "Transfer of appropriations" /*
	*/ 21 "White paper" /*
	*/ 22 "Working paper" /*
	*/ 23 "Communication" /*
	*/ 24 "Agreement" /*
	*/ 25 "Common position" /*
	*/ 26 "Joint action" /*
	*/ 27 "ECSC budget" /*
	*/ 28 "Interinstitutional agreement" /*
	*/ 29 "Act" /*
	*/ 30 "Note" /*
	*/ 31 "Treaty" /*
	*/ 32 "Member states agreement" /*
	*/ 33 "EP resolution" /*
	*/ 34 "Other" /*
	*/ , modify
	
* Loop through the different instrument variables
foreach key in adop trans com coun { 
	* Code values for instrument variable from information in type of file variable
	* Missing values
	replace inst`key' = . if ftype`key' == ""
	* Decisions
	replace inst`key' = 1 if /*
		*/ ftype`key' == "decision" | /*
		*/ ftype`key' == "draft decision" | /*
		*/ ftype`key' == "proposal for a decision" | /*
		*/ ftype`key' == "recommendation for a decision" | /*
		*/ ftype`key' == "commission:proposal for a decision" | /*
		*/ ftype`key' == "commission:proposal for a directive;council:decision"
	* Regulations
	replace inst`key' = 2 if /*
		*/ ftype`key' == "regulation" | /*
		*/ ftype`key' == "draft regulation" | /*
		*/ ftype`key' == "proposal for a regulation" | /*
		*/ ftype`key' == "recomm. for a regulation" 
	* Directives
	replace inst`key' = 3 if /*
		*/ ftype`key' == "directive" | /*
		*/ ftype`key' == "draft directive" | /*
		*/ ftype`key' == "proposal for a directive"
	* Reports
	replace inst`key' = 4 if ftype`key' == "report"
	* Declarations
	replace inst`key' = 5 if ftype`key' == "draft declaration" /*
		*/ | ftype`key' == "declaration" | ftype`key' == "proposal for a declaration"
	* Protocols
	replace inst`key' = 6 if ftype`key' == "draft protocol"
	* Recommendations
	replace inst`key' = 7 if ftype`key' == "draft recommendation" /*
		*/ | ftype`key' == "proposal for a recommendation" /*
		*/ | ftype`key' == "recomm. for a recommendation" /*
		*/ | ftype`key' == "recommendation" 
	* Resolutions
	replace inst`key' = 8 if ftype`key' == "draft resolution" /*
		*/ | ftype`key' == "proposal for a resolution" /*
		*/ | ftype`key' == "resolution"
	* Green papers
	replace inst`key' = 9 if ftype`key' == "green paper"
	* Letters
	replace inst`key' = 10 if ftype`key' == "letter"
	* Letters of amendment
	replace inst`key' = 11 if ftype`key' == "letter of amendment"
	* Memoranda
	replace inst`key' = 12 if ftype`key' == "memorandum"
	* Opinions
	replace inst`key' = 13 if ftype`key' == "opinion" | ftype`key' == "recommendation for an opinion" 
	* Periodic reports
	replace inst`key' = 14 if ftype`key' == "periodic report"
	* Budget supplements
	replace inst`key' = 15 if ftype`key' == "prelim. draft supplem. budget" /*
		*/ | ftype`key' == "supplem. and amending budet" /*
		*/ | ftype`key' == "draft suppl. and amend. budget"
	* Budgets
	replace inst`key' = 16 if ftype`key' == "preliminary draft budget" /*
		*/ | ftype`key' == "draft budget"
	* Programmes
	replace inst`key' = 17 if ftype`key' == "programme"
	* Council framework decisions
	replace inst`key' = 18 if ftype`key' == "proposal for a council framework decision" /*
		*/ | ftype`key' == "draft council framework decision" | ftype`key' == "council framework decision" 
	* Council assents
	replace inst`key' = 19 if ftype`key' == "request for council assent" /*
		*/ | ftype`key' == "council assent"
	* Transfer of appropriations
	replace inst`key' = 20 if ftype`key' == "transfer of appropriations"
	* White papers
	replace inst`key' = 21 if ftype`key' == "white paper"
	* Working papers
	replace inst`key' = 22 if ftype`key' == "working paper" | ftype`key' == "staff working paper"
	* Communications
	replace inst`key' = 23 if ftype`key' == "communication" | ftype`key' == "draft communication"
	* Agreements
	replace inst`key' = 24 if ftype`key' == "agreement" | ftype`key' == "draft agreement"
	* Common positions
	replace inst`key' = 25 if ftype`key' == "draft common position" /*
		*/ | ftype`key' == "proposal for a common position" | ftype`key' == "common position"
	* Joint actions
	replace inst`key' = 26 if ftype`key' == "joint action" | ftype`key' == "proposal for a joint action"
	* ESCS budgets
	replace inst`key' = 27 if ftype`key' == "ecsc budget" | ftype`key' == "draft ecsc budget"
	* Interinstitutional agreements
	replace inst`key' = 28 if ftype`key' == "draft interinstitut. agreement" /*
		*/ | ftype`key' == "interinstitutional agreement"
	* Acts
	replace inst`key' = 29 if ftype`key' == "proposal for an act" /*
		*/ | ftype`key' == "act" | ftype`key' == "draft act" 
	* Notes
	replace inst`key' = 30 if ftype`key' == "note"
	* Treaties
	replace inst`key' = 31 if ftype`key' == "draft treaty"
	* Agreements between member states
	replace inst`key' = 32 if ftype`key' == "agreement betw. member states" /*
		*/ | ftype`key' == "draft agreement member states"
	* EP resolutions
	replace inst`key' = 33 if ftype`key' == "resolution of ep"
	* Label instrument variable values
	label val inst`key' instrumentl
}

* Check instrument variables
tab instadop, m
tab insttrans, m
tab instcom, m
tab instcoun, m

* Generate general instrument variable from Commission adoption instrument variable
generate instrument = instadop
label value instrument instrumentl
label var instrument "Legal instrument"

* Replace missing values by values of transmission to Council instrument variable
tab instadop insttrans, m
replace instrument = insttrans if instadop == .

* Replace missing values by values of final Commission instrument variable
tab instrument instcom, m
replace instrument = instcom if instrument == .

* Replace missing values by values of final Council instrument variable
tab instrument instcoun, m
replace instrument = instcoun if instrument == .

* Code most remaining missing values from information in the title of the proposals
tab instrument, m
list webno ftypecom proccode propcode title if instrument == . & title != "NA"
replace instrument = 1 if instrument == . & regexm(title, "council decision")
replace instrument = 1 if instrument == . & regexm(title, "commission decision")
replace instrument = 1 if instrument == . & regexm(title, "proposal for a decision")
replace instrument = 1 if instrument == . & regexm(title, "proposal for a&#13;decision")
replace instrument = 1 if instrument == . & regexm(title, "council implementing decision")
replace instrument = 1 if instrument == . & regexm(title, "proposal for a&#13;council implementing decision")
replace instrument = 2 if instrument == . & regexm(title, "proposal for a&#13;regulation")
replace instrument = 2 if instrument == . & regexm(title, "^regulation")
replace instrument = 2 if instrument == . & regexm(title, "proposal for a&#13;council regulation")
replace instrument = 2 if instrument == . & regexm(title, "proposal for a council implementing regulation")
replace instrument = 2 if instrument == . & regexm(title, "proposal for a&#13;council implementing regulation")
replace instrument = 3 if instrument == . & regexm(title, "proposal for a&#13;council directive")
replace instrument = 3 if instrument == . & regexm(title, "proposal for a council directive")
replace instrument = 3 if instrument == . & regexm(title, "proposal for a&#13;directive")
replace instrument = 3 if instrument == . & regexm(title, "proposal for a directive")
replace instrument = 4 if instrument == . & regexm(title, "report from the commission")
replace instrument = 7 if instrument == . & regexm(title, "recommendation for a&#13;council recommendation")
replace instrument = 7 if instrument == . & regexm(title, "proposal for a&#13;council recommendation&")
replace instrument = 8 if instrument == . & regexm(title, "code of conduct")
replace instrument = 9 if instrument == . & regexm(title, "green paper")
replace instrument = 12 if instrument == . & regexm(title, "aide-memoire")
replace instrument = 13 if instrument == . & regexm(title, "proposal for a&#13;council opinion")
replace instrument = 13 if instrument == . & regexm(title, "commission opinion")
replace instrument = 15 if instrument == . & regexm(title, "draft amending budget")
replace instrument = 15 if instrument == . & regexm(title, "amending letter")
replace instrument = 21 if instrument == . & regexm(title, "white paper")
replace instrument = 22 if instrument == . & regexm(title, "commission staff working")
replace instrument = 22 if instrument == . & regexm(title, "commission working document")
replace instrument = 23 if instrument == . & regexm(title, "communication")
replace instrument = 29 if instrument == . & regexm(title, "council act")
tab instrument if title != "NA", m

* Generate type of legal instrument variable
generate instagg = .
* Legislative instruments
replace instagg = 1 if /*
	*/ instrument == 1 | /*
	*/ instrument == 2 | /*
	*/ instrument == 3
* Non-legislative instruments
replace instagg = 2 if /*
	*/ (instrument >= 4 & /*
	*/ instrument <= 14) | /*
	*/ instrument == 17 | /*
	*/ instrument == 21 | /*
	*/ instrument == 22 | /*
	*/ instrument == 23 | /*
	*/ instrument == 30 | /*
	*/ instrument == 33 
* Budgetary instruments
replace instagg = 3 if /*
	*/ instrument == 15 | /*
	*/ instrument == 16 | /*
	*/ instrument == 27 | /*
	*/ instrument == 20	
* Other binding instruments
replace instagg = 4 if /*
	*/ instrument == 18 | /*
	*/ instrument == 19 | /*
	*/ instrument == 24 | /*
	*/ instrument == 25 | /*
	*/ instrument == 26 | /*
	*/ instrument == 28 | /*
	*/ instrument == 29 | /*
	*/ instrument == 31 | /*
	*/ instrument == 32 
label def instaggl /*
	*/ 1 "Legislative acts" /*
	*/ 2 "Non-legislative acts" /*
	*/ 3 "Budgetary acts" /*
	*/ 4 "Other binding acts"
label val instagg instaggl
tab instagg, m


* Generate legislative procedure variable
*****************************************

* Generate Commission adoption procedure variable
rename adopByComm_0_procedures_0 procadop
replace procadop = "" if procadop == "NA"
tab procadop, m

* Generate transmission to Council procedure variable
rename tranToCoun_0_procedures_0 proctrans
replace proctrans = "" if proctrans == "NA"
tab proctrans, m

* Generate final Commission procedure variable
rename procedures_commission_0 proccom
replace proccom = "" if proccom == "NA"
tab proccom, m

* Generate final Council procedure variable
rename procedures_council_0 proccoun
replace proccoun = "" if proccoun == "NA"
tab proccoun, m

* Generate final Parliament procedure variable
rename procedures_euroParl_0 procep
replace procep = "" if procep == "NA"
tab procep, m

* Generate final member state procedure variable
rename procedures_etatMemb_0 procmemb
replace procmemb = "" if procmemb == "NA"
tab procmemb, m

* Generate general procedure variable from Commission adoption procedure variable
generate procedure = ""
replace procedure = procadop

* Replace missing values by values of transmission to Council variable
tab procadop proctrans, m
replace procedure = proctrans if procedure == ""

* Replace missing values by values of final Commission procedure variable
tab procedure proccom, m
replace procedure = proccom if procedure == ""

* Replace missing values by values of final Council procedure variable
tab procedure proccoun, m
replace procedure = proccoun if procedure == ""

* Replace missing values by values of final Parliament procedure variable
tab procedure procep, m
replace procedure = procep if procedure == ""

* Replace missing values by values of final member state procedure variable
tab procedure procmemb, m

tab proccode if procedure == ""
replace procedure = "non-legislative procedure" if procedure == "" & regexm(proccode, "nle")
replace procedure = "codecision procedure" if procedure == "" & regexm(proccode, "cod")
replace procedure = "consultation procedure" if procedure == "" & regexm(proccode, "cns")
replace procedure = "assent procedure" if procedure == "" & regexm(proccode, "app")


* Update procedure variable with information about EP readings
**************************************************************

* Rename and recode missing values of EP variables
rename tranToEp_date_0 transep
replace transep = "" if transep == "NA"
rename epCtteRepo1stRdg_date_0 epreportfirstd
replace epreportfirstd = "" if epreportfirstd == "NA"
rename epOpin1stRdg_date_0 epopinionfirstd
replace epopinionfirstd = "" if epopinionfirstd == "NA"
rename epCtteRepoSingRdg_date_0 epreportsingled
replace epreportsingled = "" if epreportsingled == "NA"
rename epOpinSingRdg_date_0 epopinionsingled
replace epopinionsingled = "" if epopinionsingled == "NA"

* Generate transmission to EP dummy variable
generate transepd = 0
replace transepd = 1 if transep ~= ""

* Generate EP first reading dummy variable
generate epfirstd = 0
replace epfirstd = 1 if (epreportfirstd ~= "" | epopinionfirstd ~= "")
label var epfirstd "EP opinion first reading (yes/no)"

* Generate EP single reading dummy variable
generate epsingled = 0
replace epsingled = 1 if (epreportsingled ~= "" | epopinionsingled ~= "")
label var epsingled "EP opinion single reading (yes/no)"

* Generate EP reading dummy variable
generate epd = 0
replace epd = 1 if epsingled == 1 | epfirstd == 1

* Generate EP involvement dummy variable
generate epinvolve = 0
replace epinvolve = 1 if epd == 1 | transepd == 1

* Check which types of procedures have EP single readings
tab procedure epsingled, row m
* EP single readings occur almost exclusively under the consultation procedure

* Check what proportion of EP single readings with known procedure are conducted under the consultation procedure
tab procedure epsingled, col
* The overwhelming majority (99%) of cases for which the procedure is known are consultation files

* Recode proposals with missing values and EP single readings to consultation
replace procedure = "consultation procedure" if epsingled == 1 & procedure == ""
tab procedure epsingled , m

* Check which types of procedures have EP first readings
tab procedure epfirstd, row m
* EP first readings occur almost exclusively under the cooperation and codecision procedure

* Check what proportion of EP first readings with known procedure are conducted under the cooperation and codecision procedure
tab procedure epfirstd, col
* EP overwhelming majority (98%) of cases for which the procedure is known are cooperation or codecision files

* Check proposals with missing values and EP first readings
format %td startdate
list webno instrument instagg proccode startdate formAdopByCoun_date_0 signByEpAndCoun_date_0 if procedure == "" & epfirstd == 1
* Most of them were either consultation or cooperation files when introduced

* Recode proposals with missing values that were introduced before 1987 to consultation files
list webno instrument proccode startdate formAdopByCoun_date_0 signByEpAndCoun_date_0 if procedure == "" & epfirstd == 1 & year <= 1987
replace procedure = "consultation procedure" if procedure == "" & epfirstd == 1 & year <= 1987
* 112 changes

* Recode remaining missing values for proposals introduced after SEA but before Maastricht came into force to cooperation files
replace procedure = "cooperation procedure" if procedure == "" & epfirstd == 1 /*
	*/ & year > 1987 & (year < 1993 | year == 1993 & month <= 10)
* 190 changes

* Recode remaining proposals with missing values introduced after Treaty of Maastricht came into force
replace procedure = "cooperation procedure" if webno == 101010 | webno == 101025 | webno == 101949
replace procedure = "codecision procedure" if webno == 11882 | webno == 20494
* the last two files left over is a budget, not a legislative file

* Code non-legislative acts as "No-procedure"
tab procedure instagg, m
replace procedure = "no procedure" if instagg == 2 | instagg == 3 | instagg == 4
tab procedure, m

* Generate procedure variable that takes procedure at start of process as correct procedure value
tab year procedure if procedure == "codecision procedure" /*
	*/ | procedure == "consultation procedure" | procedure == "cooperation procedure", m
generate procedurec = procedure

* Recode cooperation or codecision procedure values before SEA in 1987 to consultation
replace procedurec = "consultation procedure" if (year < 1987 | (year == 1987 & month < 7)) /*
	*/ & procedurec == "codecision procedure"	
replace procedurec = "consultation procedure" if (year < 1987 | (year == 1987 & month < 7)) /*
	*/ & procedurec == "cooperation procedure"	

* Recode codecision procedure values before Maastricht Treaty in 1993 to cooperation
replace procedurec = "cooperation procedure" if (year < 1993 | (year == 1993 & month < 11)) & procedurec == "codecision procedure"	

* Check cooperation procedure proposals after Amsterdam Treaty in 1999
list webno year if year > 1999 & procedurec == "cooperation procedure"
* Cooperation procedure in 2005 seems to be correct

* Check new procedure variable
tab year procedurec if procedurec == "codecision procedure" /*
	*/ | procedurec == "consultation procedure" | procedurec == "cooperation procedure", m

* Check procedure coding when there is no EP involvement
tab epinvolve procedure, m
list webno title if epinvolve == 0 & procedure == "" in 1/300
* Mostly international agreements and implementing legislation, no consultation or codecision procedures

* Recode proposals with no EP involvement to 'No procedure'
tab procedure epinvolve, m
replace procedure = "no procedure" if epinvolve == 0 & procedure == ""
replace procedurec = "no procedure" if epinvolve == 0 & procedurec == ""
tab procedure epinvolve, row m
tab procedure epinvolve, col m


* Generate decision-making event dummy variables
************************************************

* Generate EP and Council signature dummy
generate signature = 0
replace signature = 1 if signByEpAndCoun_date_0 != "NA"
label var signature "Signature by EP and Council (yes/no)"

* Generate partial EP and Council signature dummy
generate partsign = 0
replace partsign = 1 if partSignByEpAndCoun_date_0 != "NA"
label var partsign "Partial signature by EP and Council (yes/no)"

* Generate Council agreement dummy
generate cagree = 0
replace cagree = 1 if counAgre_date_0 ~= "NA"
label var cagree "Council agreement (yes/no)"

* Generate Council formal adoption dummy
generate cfadopt = 0
replace cfadopt = 1 if formAdopByCoun_date_0 ~= "NA"
label var cfadopt "Formal adoption by Council (yes/no)"

* Generate Council political agreement on common position dummy
generate cpagree = 0
replace cpagree = 1 if polAgreCommPosi_date_0 ~= "NA"
label var cpagree "Political agreement Common Position (yes/no)"

* Generate Council adoption of common position dummy
generate cpaadopt = 0
replace cpaadopt = 1 if adopCommPosi_date_0 ~= "NA"
label var cpaadopt "Adoption Common Position (yes/no)"

* Generate Council approval first reading dummy
generate capproval = 0
replace capproval = 1 if counAppr1stRdg_date_0 ~= "NA"
label var capproval "Council approval first reading (yes/no)"

* Generate Council partial adoption dummy
generate cpartadopt = 0
replace cpartadopt = 1 if partAdopByCoun_date_0 ~= "NA"
label var cpartadopt "Partial adoption by Council (yes/no)"

* Generate Council approval second reading dummy
generate capproval2 = 0
replace capproval2 = 1 if counAppr2ndRdg_date_0 ~= "NA"
label var capproval2 "Council approval second reading (yes/no)"


* Generate Council decision third reading dummy
generate capproval3 = 0
replace capproval3 = 1 if counDeciAt3rdRdg_date_0 ~= "NA"
label var capproval3 "Council approval third reading (yes/no)"

* Generate withdrawal by Commission dummy
generate withdraw = 0
replace withdraw = 1 if withByComm_date_0 ~= "NA"
label var withdraw "Withdrawal by Commission (yes/no)"

* Generate replacement dummy
generate repl = 0
replace repl = 1 if replacement_date_0 ~= "NA"
label var repl "Replacement by Commission (yes/no)"

* Generate rejection by Council dummy
tab rejeByCoun_date_0 procedure, m
* Rejections do not occur in legislative procedures

* Generate no adoption by Council dummy
tab noAdopByCoun_date_0 procedure, m
* Only three cases with legislative procedure, most likely errors

* Generate EP second reading dummy
generate epsecondd = 0
replace epsecondd = 1 if epOpin2ndRdg_date_0 ~= "NA"
label var epsecondd "EP opinion second reading (yes/no)"

* Generate EP third reading
generate epthirdd = 0
replace epthirdd = 1 if epDeciOn3rdRdg_date_0 ~= "NA"
label var epthirdd "EP opinion third reading (yes/no)"


* Further recoding of procedure variable
****************************************

* Generate general Council non-codecision adoption variable (for coding procedure variable)
generate cdec = 0
* Formal adoption by Council
replace cdec = 1 if cfadopt == 1
* Council agreement
tab cdec cagree if procedure == "", m
replace cdec = 1 if cdec == 0 & cagree == 1
* Partial adoption by Council
tab cdec cpartadopt if procedure == "", m
replace cdec = 1 if cdec == 0 & cpartadopt == 1
label value cdec yesno
tab cdec, m

* Check cases with EP transmission and Council adoption, but no EP reading
list webno title propcode proccode if cdec == 1 & procedure == "" in 1/5000
* These proposals refer mostly to international agreements, administrative or institutional decisions, or implementing law

* Code cases with EP transmission and Council adoption, but no EP reading as 'No procedure'
tab procedure cdec, m
replace procedure = "no procedure" if cdec == 1 & procedure == ""
tab procedure cdec, m
replace procedurec = "no procedure" if cdec == 1 & procedurec == ""

* Correct idiosyncratic procedure error
* This is a budgetary, not a codecision procedure (error was discovered during later stages of the analysis)
replace procedure = "" if webno == 163005
replace procedurec = "" if webno == 163005

* Examine remaining cases with EP transmission but no EP reading, and no Council adoption or signature
list title webno proccode instrument if procedure == ""
* Mostly international agreements, Commission decisions and other implementing acts, as well as budgetary proposals

* Code all remaining proposals as "No procedure"
tab procedurec, m sort
replace procedure = "no procedure" if procedure == ""
replace procedurec = "no procedure" if procedurec == ""
tab procedurec, m sort

* Generate numerical procedure variable
generate procstr = procedure
drop procedure
generate procedure = .
replace procedure = 0 if procstr == "no procedure"
replace procedure = 1 if procstr == "consultation procedure"
replace procedure = 2 if procstr == "cooperation procedure"
replace procedure = 3 if procstr == "codecision procedure"
replace procedure = 4 if procstr == "assent procedure"
replace procedure = 5 if procstr == "agreement"
replace procedure = 6 if procstr == "consultation court of auditors"
replace procedure = 7 if procstr == "consultation eur. central bank"
replace procedure = 8 if procstr == "social protocol"
replace procedure = 9 if procstr == "special legislative procedure (ep consent required)"
replace procedure = 10 if procstr == "non-legislative procedure"
drop procstr

* Label numerical procedure variable
label def procedurel /*
	*/ 0 "No procedure" /*
	*/ 1 "Consultation" /*
	*/ 2 "Cooperation" /*
	*/ 3 "Codecision" /*
	*/ 4 "Assent" /*
	*/ 5 "Agreement" /*
	*/ 6 "Consultation CoA" /*
	*/ 7 "Consultation ECB" /*
	*/ 8 "Social Protocol" /*
	*/ 9 "Special Legislative" /*
	*/ 10 "Non-Legislative" /*
	*/ 11 "Other", modify
label val procedure procedurel
label var procedure "Legislative procedure"
tab procedure, m sort

* Generate numerical procedure variable (procedure coded at start of process)
generate procstr = procedurec
drop procedurec
generate procedurec = .
replace procedurec = 0 if procstr == "no procedure"
replace procedurec = 1 if procstr == "consultation procedure"
replace procedurec = 2 if procstr == "cooperation procedure"
replace procedurec = 3 if procstr == "codecision procedure"
replace procedurec = 4 if procstr == "assent procedure"
replace procedurec = 5 if procstr == "agreement"
replace procedurec = 6 if procstr == "consultation court of auditors"
replace procedurec = 7 if procstr == "consultation eur. central bank"
replace procedurec = 8 if procstr == "social protocol"
replace procedurec = 9 if procstr == "special legislative procedure (ep consent required)"
replace procedurec = 10 if procstr == "non-legislative procedure"
drop procstr

* Label numerical procedure variable
label val procedurec procedurel
label var procedurec "Legislative procedure (start)"
tab procedurec, m sort

* Drop irrelevant event variables
drop adopAmenProp* tranToEp* typeOfFile* adopByComm* chanOfLegaBasiByComm* /*
	*/ commPosiOnEpAmenOn1stRead* epCtteRepo1stRdg* epCtteRepoSingRdg* epOpin1stRdg* epOpinSingRdg* /*
	*/ rejeByCoun* commPosiOnEpAmenOnSingRead* tranAmenPropToCoun* tranAmenPropToCoun* tranAmenPropToEp* /*
	*/  epDeciOn3rdRdg* epOpin2ndRdg* noAdopByCoun* tranToCoun*

* Keep only relevant variables	
keep proccode propcode title webno dateadop datetransc datetranse startdate year month year2 instrument procedurec /*
	*/ epsingled epfirstd epsecondd epthirdd signature partsign /*
	*/ cagree cfadopt cpagree cpaadopt capproval capproval2 capproval3 cpartadopt withdraw repl *_date_*

* Order the remaining variables
order webno propcode proccode title dateadop datetransc datetranse startdate year month year2 instrument procedurec /*
	*/ cagree cfadopt cpartadopt cpagree cpaadopt capproval capproval2 capproval3 /*
	*/ epsingled epfirstd epsecondd epthirdd signature partsign repl withdraw *_date_*
	
* Sort and save dataset
sort webno
compress
des, short
save "prlx-v05-management02.dta", replace
log close
exit
